package com.xiaowenxiong.controller;

import com.xiaowenxiong.model.User;


import com.xiaowenxiong.model.UserException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by 15c on 2017/3/29.
 */
@Controller
@RequestMapping("/user")
public class UserController {

      //对象集合
        private Map<String,User> users=new HashMap<String, User>();
        public UserController(){
            users.put("1",new User("1","jie1","cooljie1","wajfjfji154"));
            users.put("2",new User("2","jie2","cooljie2","cjdcjdn"));
            users.put("3",new User("3","jie3","cooljie3","cjcdcsd"));
            users.put("4",new User("4","jie4","cooljie4","cdjhudhua"));
        }

     /**
     * 列出所有用户的信息
     * @param model
     * @return /user/list
     */
        @RequestMapping(value = "/users",method = RequestMethod.GET)
        public  String listUser(Model model){
            model.addAttribute("users", users);
            return "user/list";
        }

    /**
     *点击list中的添加用户按钮跳转到add.jsp页面
     * @param model
     * @return user/add
     */
    @RequestMapping(value = "/add",method = RequestMethod.GET)
    public String adduser(Model model){
       model.addAttribute(new User());
        return "user/add";
    }

    /**BindingResult bindingResult 必须紧跟着验证参数
     * 在add页面中，添加完数据后点击添加按钮，将会跳转到list.jsp页面
     * @param user 用户对象
     * @param bindingResult  用于验证
     * @return redirect:/user/users
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public String addUser(@Validated User user,BindingResult bindingResult,MultipartFile[] attachs,HttpServletRequest req){
        System.out.print(user.toString());
        if (bindingResult.hasErrors()){
            return "user/add";
        }
        for(MultipartFile attach: attachs){
            if (attach.isEmpty()) {continue;}

        }
        users.put(user.getId(), user);
        return "redirect:/user/users";
    }
    /**
     * 学生信息查看
     *
     * @return /user/username
     */
    @RequestMapping(value = "/{uname}", method = RequestMethod.GET)
    public String showUser(Model model, @PathVariable String uname) {
        System.out.println(uname);
        System.out.println(users.get(uname));
        model.addAttribute("user", users.get(uname));
        return "user/user";
    }
    /**
     * 删除信息
     */
    @RequestMapping(value = "/{id}/delete",method = RequestMethod.GET)
    public String delUser(@PathVariable String id) {
        System.out.println("点击对象为："+id);
        users.remove(id);
        return "redirect:/user/users";

    }

    /**
     * 更新用户
     * @param model
     * @param uname
     * @return
     */
    @RequestMapping(value = "/{uname}/update",method = RequestMethod.GET)
    public  String updateUser(Model model,@PathVariable String uname){
        model.addAttribute("user", users.get(uname));
        return "user/update";
    }

    @RequestMapping(value = "/{uname}/update",method = RequestMethod.POST)
    public  String updateUsers(@Validated User user,BindingResult bindingResult){

        if (bindingResult.hasErrors()) return "user/update";

      users.put(user.getId(), user);
        return "redirect:/user/users";
    }

    /**
     * 登录
     * @param uname
     * @param password
     * @param httpSession
     * @return
     */
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public String login(String uname,String password,HttpSession httpSession){
        System.out.println("uname:"+uname+"password:"+password);
        if (!users.containsKey(uname)){
            try {
                throw new UserException("该用户不存在!");
            } catch (UserException e) {
                e.printStackTrace();
            }

        }
        if (!users.get(uname).getPassword().equals(password)){
            try {
                throw new UserException("密码不正确！");
            } catch (UserException e) {
                e.printStackTrace();
            }
        }
        httpSession.setAttribute("LoginUser",users.get(uname));
        return "redirect:/user/users";
    }

    /**
     *
     */
}

